<template>
  <view>
    <customtop top_title="个人主页"></customtop>
    <view class="user_show">
      <view class="" v-if="!loading">
        <view class="pagebox top_page">
          <image mode="aspectFill" :src="user.avatar" class="cover" />
          <view class="name">
            <span class="txt">{{user.nickname}}</span>
            <span class="auth_icons">
              <i class="iconfont icon-viphuiyuan1 auth_icon_vip" v-if="user.vip == 1"></i>
              <i class="iconfont icon-shimingrenzheng auth_icon_realname" v-if="user.realname_auth == 1"></i>
              <i class="iconfont icon-qiyerenzheng auth_icon_company" v-if="user.company_auth == 1"></i>
            </span>
          </view>
          <view class="types">
            <span class="type">{{user.city_shortname}}</span>
            <span class="type" v-if="user.company_auth == 1">{{user.company_shortname}}</span>
            <span class="type" v-if="user.job_title">{{user.job_title}}</span>
          </view>
        </view>
        <view class="pagebox company">
          <view class="stitle"><span class="txt">企业信息</span></view>
          <view class="bd" @click="jumpPage('/pages/company/show?id=' + company.id)" v-if="user.company_auth == 1 && company.id">
            <view class="main">
              <image class="company_logo" :src="company.logo" />
              <view class="info">
                <view class="company_shortname">{{company.shortname}}</view>
                <view class="dec">{{company.name}}</view>
                <view class="dec">{{company.industry}}行业</view>
                <view class="dec">{{company.user_count}}个员工，{{company.post_count}}条发布信息</view>
              </view>
              <view class="more">
                <i class="iconfont icon-youbian"></i>
              </view>
            </view>
          </view>
          <view class="bd noauth" v-if="user.company_auth == 0">
            暂未认证企业信息~
          </view>
        </view>
        <view class="pagebox post_list2">
          <view class="stitle"><span class="txt">发布记录</span></view>
          <view class="bd" v-if="!data_list_loading">
            <view v-if="data_list.length > 0">
              <view class="items">

                <view class="item" v-for="(item, index) in data_list" :key="index" @click="jumpPage('/pages/post/show?id=' + item.id)">
                  <view class="top">
                    <view class="title">{{item.title}}</view>
                  </view>
                  <view class="types">
                    <view class="type_item">
                      <span>编号：</span><span>{{item.id}}</span>
                    </view>
                    <view class="type_item">
                      <span>类目：</span><span>{{item.category_name}}</span>
                    </view>
                  </view>
                </view>
              </view>
              <view class="uloadmore" @click="getMore"><u-loadmore :status="loadmore_status" /></view>
            </view>
            <u-empty
              v-if="data_list.length == 0"
              mode="data"
              icon="http://cdn.uviewui.com/uview/empty/data.png"
              text="暂无内容~"
            >
            </u-empty>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { request } from "@/utils/http.js"
import customtop from "@/components/customtop.vue"
export default {
  components: { customtop },

  data() {
    return {
      loading: true,
      user: {},
      data_list: [],
      data_list_loading: true,
      loadmore_status: 'loadmore',
      loadmore_finished: false,
      params: {
        page_size: 15,
        page: 1,
      },
      company: {}
    }
  },

  onLoad(options) {
    uni.showLoading();
    this.id = options.id;
    this.getUser();
    this.getPostsPaginate();
  },

  onReachBottom() {
    this.getMore();
  },

  methods: {
    getUser: function() {
      request.post('/user/getUser', {id: this.id}).then(res => {
        uni.hideLoading();
        this.loading = false;
        this.user = res.data;
        if (res.data.company_auth == 1) {
          this.getCompany();
        }
      })
    },

    getCompany: function() {
      request.post('/company/getCompany', {id: this.user.company_id}).then(res => {
        uni.hideLoading();
        this.loading = false;
        this.company = res.data;
      })
    },

    getPostsPaginate: function() {
      let params = this.params;
      params.user_id = this.id;
      request.post('/post/getPostsPaginate', params).then(res => {
        uni.stopPullDownRefresh();
        uni.hideLoading();
        this.data_list_loading = false;

        // 返回数据为空
        if (res.data.total == 0) {
          this.data_list = [];
          return false;
        }

        // 组装数据
        if (res.data.current_page == 1) {
          this.data_list = res.data.data;
        } else {
          this.data_list = this.data_list.concat(res.data.data);
        }

        // 最后一页
        if (this.params.page == res.data.last_page) {
          this.loadmore_finished = true;
          this.loadmore_status = 'nomore';
          return false;
        }

        this.params.page = parseInt(res.data.current_page) + parseInt(1);
        this.loadmore_status = 'loadmore';
        this.loadmore_finished = false;
      })
    },

    getMore: function() {
      if (!this.loadmore_finished) {
        this.loadmore_status = 'loading';
        this.getPostsPaginate();
      }
    },

    switchTab: function(url) {
      uni.switchTab({ url: url });
    },

    jumpPage: function(url) {
      uni.navigateTo({ url: url });
    }
  }
}
</script>

<style>
@import url("user.css");
</style>
